'use strict';

/**
 * Config for the router
 */
angular.module('app')
    .run(
        ['$rootScope', '$state', '$stateParams',
            function ($rootScope, $state, $stateParams) {
                $rootScope.$state = $state;
                $rootScope.$stateParams = $stateParams;
            }
        ]
    )
    .config(
        ['$stateProvider', '$urlRouterProvider', 'JQ_CONFIG', 'MODULE_CONFIG',
            function ($stateProvider, $urlRouterProvider, JQ_CONFIG, MODULE_CONFIG) {
                if (window.location.href.indexOf('showindex') != -1) {
                    var layout = "tpl/layout/show_wx.html";
                } else {
                    var layout = "tpl/layout/wx.html";
                }
                $urlRouterProvider.otherwise('/wx/reserveIndex');

                $stateProvider
                    .state('wx', {
                        abstract: true,
                        url: '/wx',
                        templateUrl: loadHtml(layout)
                    })
                    .state('wx.test', {
                        url: '/test',
                        templateUrl: loadHtml('tpl/wx/test.html'),
                        resolve: load(['js/controllers/wx/test.js'])
                    })
                    .state('wx.store_list', {
                        url: '/store_list',
                        templateUrl: loadHtml('tpl/wx/store_list.html'),
                        resolve: load(['js/controllers/wx/store_list.js'])
                    })
                    .state('wx.order_dishes', {
                        url: '/order_dishes?type&mode&billid&code&tableno',
                        templateUrl: loadHtml('tpl/wx/order_dishes.html'),
                        resolve: load(['js/controllers/wx/order_dishes.js'])
                    })
                    .state('wx.pay_bill', {
                        url: '/pay_bill?billid&tableno&code',
                        templateUrl: loadHtml('tpl/wx/pay_bill.html'),
                        resolve: load(['js/controllers/wx/pay_bill.js'])
                    })
                    .state('wx.pay_result', {
                        url: '/pay_result?id',
                        templateUrl: loadHtml('tpl/wx/pay_result.html'),
                        resolve: load(['js/controllers/wx/pay_result.js'])
                    })
                    .state('wx.index', {
                        url: '/index',
                        templateUrl: loadHtml('tpl/wx/card_index.html'),
                        resolve: load(['js/controllers/wx/card_index.js'])
                    })
                    .state('wx.address', {
                        url: '/address',
                        templateUrl: loadHtml('tpl/wx/card_address.html'),
                        resolve: load(['js/controllers/wx/card_address.js'])
                    })
                    .state('wx.address_add', {
                        url: '/address_add',
                        templateUrl: loadHtml('tpl/wx/card_address_add.html'),
                        resolve: load(['js/controllers/wx/card_address_add.js'])
                    })
                    .state('wx.file', {
                        url: '/file',
                        templateUrl: loadHtml('tpl/wx/card_file.html'),
                        resolve: load(['js/controllers/wx/card_file.js'])
                    })
                    .state('wx.storevalue', {
                        url: '/storevalue?Type&&CardID',
                        templateUrl: loadHtml('tpl/wx/card_storevalue.html'),
                        resolve: load(['js/controllers/wx/card_storevalue.js'])
                    })
                    .state('wx.points', {
                        url: '/points',
                        templateUrl: loadHtml('tpl/wx/card_my_points.html'),
                        resolve: load(['js/controllers/wx/card_my_points.js'])
                    })
                    .state('wx.coupon', {
                        url: '/coupon',
                        templateUrl: loadHtml('tpl/wx/card_coupons.html'),
                        resolve: load(['js/controllers/wx/card_coupon.js'])
                    })
                    .state('wx.coupon_detail', {
                        url: '/coupon_detail',
                        templateUrl: loadHtml('tpl/wx/card_coupons_detail.html'),
                        resolve: load(['js/controllers/wx/card_coupon_detail.js'])
                    })
                    .state('wx.address_edit', {
                        url: '/address_edit',
                        templateUrl: loadHtml('tpl/wx/card_address_edit.html'),
                        resolve: load(['js/controllers/wx/card_address_edit.js'])
                    })
                    .state('wx.points_charge', {
                        url: '/points_charge',
                        templateUrl: loadHtml('tpl/wx/card_points_charge.html'),
                        resolve: load(['js/controllers/wx/card_points_charge.js'])
                    })
                    .state('wx.orders', {
                        url: '/orders',
                        templateUrl: loadHtml('tpl/wx/card_orders.html'),
                        resolve: load(['js/controllers/wx/card_orders.js'])
                    })
                    .state('wx.order_details', {
                        url: '/order_details?billid',
                        templateUrl: loadHtml('tpl/wx/card_orderDetails.html'),
                        resolve: load(['js/controllers/wx/card_orderDetails.js'])
                    })
                    .state('wx.reserve', {
                        url: '/reserve',
                        templateUrl: loadHtml('tpl/wx/reserve.html'),
                        resolve: load(['js/controllers/wx/reserve.js'])
                    })
                    .state('wx.register', {
                        url: '/register?backurl',
                        templateUrl: loadHtml('tpl/wx/card_register.html'),
                        resolve: load(['js/controllers/wx/card_register.js'])
                    })
                    .state('wx.points_charge_list', {
                        url: '/points_charge_list',
                        templateUrl: loadHtml('tpl/wx/card_points_charge_list.html'),
                        resolve: load(['js/controllers/wx/card_points_charge_list.js'])
                    })
                    .state('wx.redirect', {
                        url: '/redirect?url',
                        templateUrl: loadHtml('tpl/wx/redirect.html'),
                        resolve: load(['js/controllers/wx/redirect.js'])
                    })
                    .state('wx.activityList', {
                        url: '/activityList?type',
                        templateUrl: loadHtml('tpl/wx/activity_list.html'),
                        resolve: load(['js/controllers/wx/activity_list.js'])
                    })
                    .state('wx.activityDetail', {
                        url: '/activityDetail?id',
                        templateUrl: loadHtml('tpl/wx/activity_detail.html'),
                        resolve: load(['js/controllers/wx/activity_detail.js'])
                    })
                      .state('wx.activityMyDetail', {
                          url: '/activityMyDetail?id',
                          templateUrl: loadHtml('tpl/wx/activity_mydetail.html'),
                          resolve: load(['js/controllers/wx/activity_mydetail.js'])
                      })
                    .state('wx.question', {
                        url: '/question?billno',
                        templateUrl: loadHtml('tpl/wx/question.html'),
                        resolve: load(['js/controllers/wx/question.js'])
                    })
                    .state('wx.submitResult', {
                        url: '/submitResult?type',
                        templateUrl: loadHtml('tpl/wx/submit_result.html'),
                        resolve: load(['js/controllers/wx/submit_result.js'])
                    })
                    .state('wx.activityEnter', {
                        url: '/activityEnter?id',
                        templateUrl: loadHtml('tpl/wx/activity_enter.html'),
                        resolve: load(['js/controllers/wx/activity_enter.js'])
                    })
                    .state('wx.reserveIndex', {
                        url: '/reserveIndex',
                        templateUrl: loadHtml('tpl/wx/reserve_index.html'),
                        resolve: load(['js/controllers/wx/reserve_index.js', 'swiper'])
                    })
                    .state('wx.reserveOrders', {
                        url: '/reserveOrders',
                        templateUrl: loadHtml('tpl/wx/reserve_orders.html'),
                        resolve: load(['js/controllers/wx/reserve_orders.js'])
                    })
                    .state('wx.reserveOrder', {
                        url: '/reserveOrder?type&mode&billid&code&tableno&reservetime',
                        templateUrl: loadHtml('tpl/wx/reserve_order.html'),
                        resolve: load(['js/controllers/wx/reserve_order.js'])
                    })
                    .state('wx.reservePayResult', {
                        url: '/reservePayResult?billid',
                        templateUrl: loadHtml('tpl/wx/reservePay_result.html'),
                        resolve: load(['js/controllers/wx/reservePay_result.js'])
                    })
                     .state('wx.addWXCard', {
                         url: '/addWXCard?cardID',
                         templateUrl: loadHtml('tpl/wx/addWXCard.html'),
                         resolve: load(['js/controllers/wx/addWXCard.js'])
                     })
                    .state('wx.editor', {
                        url: '/editor?id',
                        templateUrl: loadHtml('tpl/wx/editor.html'),
                        resolve: load(['js/controllers/wx/editor.js'])
                    });;


                function loadHtml(_src) {
                    if (_.startsWith(_src, 'tpl/'))
                        _src = _src + '?__t=' + new Date().getTime();

                    return _src;
                }

                /*  function load(_srcs, callback) {
                      return {
                          deps: ['$ocLazyLoad', '$q',
                              function ($ocLazyLoad, $q) {
                                  var deferred = $q.defer();
                                  var promise = deferred.promise;
                                  _srcs = angular.isArray(_srcs) ? _srcs : _srcs.split(/\s+/);
                                  angular.forEach(_srcs, function (_src) {
                                      promise = promise.then(function () {
                                          if (_.startsWith(_src), 'js/')
                                              _src = _src + '?__t=' + new Date().getTime();
  
                                          return $ocLazyLoad.load(_src);
                                      });
                                  });
                                  deferred.resolve();
                                  return callback ? promise.then(function () {
                                      return callback();
                                  }) : promise;
                              }]
                      }
                  }*/
                function load(srcs, callback) {
                    return {
                        deps: ['$ocLazyLoad', '$q',
                            function ($ocLazyLoad, $q) {
                                var deferred = $q.defer();
                                var promise = false;
                                srcs = angular.isArray(srcs) ? srcs : srcs.split(/\s+/);
                                if (!promise) {
                                    promise = deferred.promise;
                                }
                                angular.forEach(srcs, function (src) {
                                    promise = promise.then(function () {
                                        if (JQ_CONFIG[src]) {
                                            return $ocLazyLoad.load(JQ_CONFIG[src]);
                                        }
                                        angular.forEach(MODULE_CONFIG, function (module) {
                                            if (module.name == src) {
                                                name = module.name;
                                            } else {
                                                name = src;
                                            }
                                        });
                                        return $ocLazyLoad.load(name);
                                    });
                                });
                                deferred.resolve();
                                return callback ? promise.then(function () { return callback(); }) : promise;
                            }]
                    }
                }

            }]);
